import java.util.Arrays;

public class Test {
    public static void main(String[] args) {
        int[][] array = {{1,2,3},{4,5,6}};

        System.out.println(array[1][1]);
        System.out.println(array[1][0]);

        for (int i = 0; i < 2; i++) {
            for (int j = 0; j < 3; j++) {
                System.out.println(array[i][j]);
            }
            System.out.println();
        }

        int[][] array2 = new int[][] {{1,2,3},{4,5,6}};

        //Java 当中的二维数组 是可以省略列的
        int[][] array3 = new int[2][3];
    }



    //==================以下为作业============================

     //找出数组中两个元素相加等于特定值的两个元素
    public static int[] twoIndex(int[] array,int target) {
        int[] tmp = {-1,-1};
        for (int i = 0; i < array.length; i++) {
            for (int j = i+1; j < array.length; j++) {
                if(array[i] + array[j] == target) {
                    tmp[0] = i;
                    tmp[j] = j;
                }
            }
        }
        return tmp;
    }

    //找出数组中只出现一次的元素，其他元素都只出现两次
    public static int singleNum(int[] array) {
        int ret = 0;
        for (int i = 0; i < array.length; i++) {
            ret ^= array[i];
        }
        return ret;
    }

    //找出数组中出现次数超过 n/2 的元素
    public static int moreNum(int[] array) {
        Arrays.sort(array);

        return array[array.length/2];
    }

    //是否有连续3个奇数
    public static boolean function(int[] array) {
        int count = 0;
        for (int i = 0; i < array.length; i++) {
            if(array[i] % 2 != 0) {
                //遇到奇数了
                count++;
                if(count == 3) {
                    return true;
                }
            }else {
                //遇见偶数了
                count = 0;
            }
        }
        return false;
    }

    public static void main12(String[] args) {
        int[] array = {1,3};
        boolean flg = function(array);

        int ret = singleNum(array);
        System.out.println(ret);
        int[] ret2 = twoIndex(array,18);

        System.out.println(Arrays.toString(ret2));

    }

    //奇数在前 偶数在后
    public static void func(int[] array) {
        int i = 0;
        int j = array.length-1;
        while(i < j) {
            while(i < j && array[i]%2 != 0) {
                i++;
            }
            while(i < j && array[j]%2 == 0) {
                j--;
            }
            swap(array,i,j);
        }
    }

    public static void main11(String[] args) {
        int[] array = {1,4,3,2,5};

        func(array);

        System.out.println(Arrays.toString(array));
    }

    //2倍
    public static void transform1(int[] array) {
        for (int i = 0; i < array.length; i++) {
            array[i] = array[i]*2;
        }
    }
    public static int[] transform2(int[] array) {
        int[] tmp = new int[array.length];
        for (int i = 0; i < array.length; i++) {
            tmp[i] = array[i]*2;
        }
        return tmp;
    }

    public static void main10(String[] args) {
        int[] array = {1,2,3,4,5};
        //transform1(array);
        int[] ret = transform2(array);
        System.out.println(Arrays.toString(array));
        System.out.println("ret: "+Arrays.toString(ret));
    }

    //比较数组是否相等与填充数组
    public static void main9(String[] args) {
        //比较数组是否相等
        int[] array1 = {1,21,31,3,5};
        int[] array2 = {1,21,31,3,51};
        boolean flg = Arrays.equals(array1,array2);
        System.out.println(flg);

        //填充数组
        System.out.println("========fill========");
        int[] array3 = new int[10];
        //Arrays.fill(array3,2,5,8);
        Arrays.fill(array3,2,5,8);
        System.out.println(Arrays.toString(array3));
    }

    //二分查找和冒泡排序
    public static int find(int[] array, int key) {
        for (int i = 0; i < array.length; i++) {
            if(array[i] == key) {
                return i;
            }
        }
        return -1;
    }

    public static int binarySearch(int[] array, int key) {
        int left = 0;
        int right = array.length-1;

        while(left <= right) {
            int mid = (left+right)/2;
            if(array[mid] < key) {
                left = mid + 1;
            }else if(array[mid] == key) {
                return mid;
            }else {
                right = mid - 1;
            }
        }
        return -1;
    }
    private static void swap(int[] array, int i,int j) {
        int tmp = array[j];
        array[j] = array[i];
        array[i] = tmp;
    }

    public static void bubbleSort(int[] array) {
        for (int i = 0; i < array.length - 1; i++) {
            boolean flg = false;
            //- i 就是一种优化  每一趟都比上一趟少比较一次
            for (int j = 0; j < array.length - 1 - i; j++) {
                if(array[j] > array[j+1]) {
                    swap(array,j,j+1);
                    flg = true;
                }
            }
            if(flg == false) {
                break;
            }
        }
    }

    public static void main8(String[] args) {
        int[] array = {1,21,31,3,5};

        //bubbleSort(array);
        //Arrays.sort(array,2,5);
        Arrays.sort(array);

        System.out.println("排列之后的数组： "+Arrays.toString(array));

        //int index = find(array,3);
        //int index = binarySearch(array,3);
        int index = Arrays.binarySearch(array,13);

        if(index == -1) {
            System.out.println("没有找到你要找的数字！");
        }else {
            System.out.println("找到你要找的数字了！下标为："+index);
        }

    }

    //给定范围的拷贝
    public static void main7(String[] args) {
        int[] array = {1,2,3,4,5};

        //Java中 一般下标给定范围时都是左闭右开[1,3)
        int[] copy = Arrays.copyOfRange(array,1,3);

        System.out.println(Arrays.toString(copy));//[2, 3]
    }

    //扩容
    public static void main6(String[] args) {
        int[] array = {1,2,3,4,5};

        int[] copy = Arrays.copyOf(array,array.length*2);
        array = copy;

        for(int x:copy) {
            System.out.print(x+" ");//1 2 3 4 5 0 0 0 0 0
        }
        System.out.println();

        System.out.println(Arrays.toString(array));//[1, 2, 3, 4, 5, 0, 0, 0, 0, 0]

    }

    //第三种拷贝方式
    public static void main5(String[] args) {
        int[] array = {1,2,3,4,5};

        int[] copy = new int[array.length];

        //帮我们完成了拷贝
        System.arraycopy(array,0,copy,0,array.length);

        for(int x:copy) {
            System.out.print(x+" ");
        }
        System.out.println();
    }

    //第二种拷贝方法
    public static void main4(String[] args) {
        int[] array = {1,2,3,4,5};

        int[] copy = Arrays.copyOf(array,array.length);

        for (int x:copy) {
            System.out.print(x+" ");
        }
        System.out.println();
    }

    //与拷贝做比较
    public static void main3(String[] args) {
        //这个代码不是拷贝
        int[] array = {1,2,3,4,5};
        int[] array2 = array;

        for(int x:array2) {
            System.out.print(x+" ");
        }
        System.out.println();
    }

    //拷贝数组
    public static void main2(String[] args) {
        int[] array = {1,2,3,4,5};

        int[] copy = new int[array.length];

        for(int i = 0; i < array.length; i++) {
            copy[i] = array[i];
        }

        for(int x:copy) {
            System.out.print(x+" ");
        }
        System.out.println();
    }

    //打印数组的两种方法，把数组转化成数组并打印
    public static void main1(String[] args) {
        int[] array = {1,2,3,4,5};
        for(int i = 0; i < array.length; i++) {
            System.out.print(array[i]+" ");
        }
        System.out.println();
        System.out.println("================");
        for(int x : array) {
            System.out.print(x+" ");
        }
        System.out.println();
        System.out.println("================");

        String ret = Arrays.toString(array);
        System.out.println(ret);
    }
}
